import sys
# 将子模块的路径添加到 Python 的模块搜索路径中
sys.path.append('../dxt')

from lin_base import *
from starsln import starsln
from starsi import starsi
from csts import CSTS

MAG_N_R = 12
MAG_0_R = 11
MAG_1_R = 9
MAG_2_R = 7
MAG_3_R = 5
MAG_4_R = 3
MAG_5_R = 2
MAG_X_R = 1

def mag_to_r(mag):
    if mag <0:
        r=MAG_N_R  #12
    elif mag <1:
        r=MAG_0_R  #11
    elif mag <2:
        r=MAG_1_R  #9
    elif mag <3:
        r=MAG_2_R   #7
    elif mag <4:
        r=MAG_3_R   #5
    elif mag <5:
        r=MAG_4_R   #3
    else:
        r=MAG_5_R   #2
    
    return round(r *1.6)

SP_COLORS={
    'O': (0,183,235,255),
    'B':(0,255,255,255),
    'A':(224,255,255,255),
    'F':(255,255,255,255),
    'G':(238,255,27,255),
    'K':(255,140,0,255),
    'M':(255,0,0,255),
}

def SP_to_color(sp):
    if sp in SP_COLORS:
        return SP_COLORS[sp]
    return (255,255,255,0)

    
def draw_star_with_ra(draw,star,ra):
    FCOLOR=(0,0,0,255)
    #BGCOLOR=(255,255,255,0)
    #LW=4
    ra_deg,dec,mag,sp= starsi[star]
    #ra=ra_deg/15.0
    y = dec_to_y(dec)
    x = RA_to_x(g_share.x0, ra)
    if x < g_share.x0:
        return None,None
    if x > g_share.xr_end:
        return None,None
    #x,y =ra_dec_to_xyplot(Ra,decl,xc,yc,rr)
    #if star not in stars_plotted:
    r = mag_to_r(mag) 
    co = SP_to_color(sp)
    if sp=='A' or sp=='F' or r >3:
        #x,y=draw_obj(draw, ra,dec, r_deg, FCOLOR,co)
        draw.circle((x,y),r,outline=FCOLOR, fill=co,width=2)
    else:
        #x,y=draw_obj(draw, ra,dec, r_deg, co,co)
        draw.circle((x,y),r,outline=co, fill=co,width=2)
        g_share.stars_plotted.append(star)
    return x,y
    
def draw_star(draw, star): #,xc,yc,rr,
              #small=False,color_f=True):
    FCOLOR=(0,0,0,255)
    #BGCOLOR=(255,255,255,0)
    #LW=4
    ra_deg,dec,mag,sp= starsi[star]
    ra=ra_deg/15.0
    y = dec_to_y(dec)
    
    xs =[]
    x0 = RA_to_x(g_share.x0, ra)
    x1 = RA_to_x(g_share.x0, ra+24)
    for x in [x0,x1]:
        if x > g_share.x0 and x < g_share.xr_end:
            xs.append(x)
            
    #x,y =ra_dec_to_xyplot(Ra,decl,xc,yc,rr)
    #if star not in stars_plotted:
    r = mag_to_r(mag)
    co = SP_to_color(sp)
    if sp=='A' or sp=='F' or r >3:
        #x,y=draw_obj(draw, ra,dec, r_deg, FCOLOR,co)
        for x in xs:
            draw.circle((x,y),r,outline=FCOLOR, fill=co,width=2)
        #draw.circle((x1,y),r,outline=FCOLOR, fill=co,width=2)    
    else:
        #x,y=draw_obj(draw, ra,dec, r_deg, co,co)
        for x in xs:
            draw.circle((x,y),r,outline=co, fill=co,width=2)
        #draw.circle((x1,y),r,outline=co, fill=co,width=2)
        #g_share.stars_plotted.append(star)
    return x,y
    
class GROUP_LINE:  # split line to different region
    def __init__(self,cst):
        self.cst=cst
        self.cst_lines=[]
        self.line=[]  # list of (star_hr,ra)
        self.line_0=[]
        
        
    def feed_line_of_stars(self, lstars):
        for star in lstars:
            ra_deg,dec,mag,sp= starsi[star]
            ra = ra_deg/15.0
            if self.cst in ['Tuc','Peg','Psc','Scl']: # special handle for wrap back to 0
                if ra < 20:
                    ra+=24
                    
            if ra >18.0:
                self.line_0.append((star,ra))
                pass
            ra += 24
            self.line.append((star,ra))
       
        
    def lines(self):
        if len(self.line_0) >0:
            return [self.line_0,self.line]
        return [self.line]
    
def group_cst_line(cst,lstars):
    group_line = GROUP_LINE(cst)
    group_line.feed_line_of_stars(lstars)

    return group_line.lines()

def draw_cst(draw, cst):
    cst_lines=[]
    for lstars in starsln[cst]:
        nlstars =group_cst_line(cst,lstars)
        cst_lines+= nlstars
        
    for nlstars in cst_lines:
        ls =1
        for star,ra in nlstars:
            x,y = draw_star_with_ra(draw,star,ra)
            if x is None:
                ls=1
                continue
            if ls==1:
                ls=0
                x0=x
                y0=y
            else:
                #if cst !='UMi' and cst != 'Oct':  
                draw.line([(x0,y0),(x,y)],fill=(60,60,60,255),width=2)  
                x0=x
                y0=y
    
    # draw stars over again
    for nlstars in cst_lines:
        for star,ra in nlstars:
            draw_star_with_ra(draw,star,ra)
            #print('ra: %.2f' % ra)
    
    
def draw_fix_stars(paper):
    layer = paper.add_layer(name='fix_stars')
    draw = layer.draw
    g_share.stars_plotted=[]

    # plot stars and lines in constellations                
    for cst in CSTS:
        draw_cst(draw, cst)
        
    #print('cst 81:', CSTS[81])
    # plot stars not in cst lines
    stars = list(starsi.keys())
    for star in stars:
        if star not in g_share.stars_plotted:
            if star==1948:
                print('star 1948 found')
            draw_star(draw, star)
            